package emulator.hardware.bus;

import emulator.hardware.debug.BusWatchException;
import emulator.hardware.memory.MemoryBlockInterface;
import emulator.hardware.memory.UnmappedMemoryException;

public class VideoBus implements Bus {
	
	Bus bus;

	public VideoBus(Bus bus)
	{
		this.bus = bus;
	}
	
	@Override
	public int read(int address) throws BusWatchException,
			UnmappedMemoryException {
		return bus.read(convertAddress(address));
	}

	@Override
	public void write(int address, int data) throws BusWatchException,
			UnmappedMemoryException {
		bus.write(convertAddress(address), data);
	}

	public static int convertAddress(int address) {
		int target = address & 0x1FFF;
		if ((address & 0x2000) == 0)
			target += 0x8000;
		return target;
	}
	
	public byte[] getMemory()
	{
		byte [] mem_block = null;
		try {
			MemoryBlockInterface block = (MemoryBlockInterface)bus;
			mem_block = block.getData();
		} catch (ClassCastException e) {}
		return mem_block;
	}
}
